<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;
use yii\helpers\Html;

class Faq extends Basic
{
    
    public static function tableName()
    {
        return '{{%faq}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['question', 'string', 'message' => 'question必须是字符串',
                                'min' => 1, 'tooShort' => 'question最短1位',
                                'max' => 256, 'tooLong' => 'question最长256位'],
            ['answer', 'string', 'message' => 'answer必须是字符串',
                                'min' => 1, 'tooShort' => 'answer最短1位',
                                'max' => 512, 'tooLong' => 'answer最长512位'],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(1, 2), 'message' => 'display值必须在1,2中选择'], 
            ['sort', 'integer', 'message' => 'sort必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'sort最小存储0', 
                     'max'=>18446744073709551615, 'tooBig' => 'sort最大存储18446744073709551615'],
            ['click_count', 'integer', 'message' => 'click_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'click_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'click_count最大存储18446744073709551615'] 
        ];
    }


    /*添加*/
    public function addFaq($data, $app_id)
    {
        // P($data);
        $this->scenario = 'add';
        $data['Faq']['app_id'] = $app_id;
        if(isset($data['Faq']['answer'])){
            $data['Faq']['answer'] = Html::encode($data['Faq']['answer']);
        }
        if(isset($data['Faq']['display'])){
            $data['Faq']['display'] = Tool::changeBool($data['Faq']['display']);
        }
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                $this->updateAll(['sort' => $id], 'id = :id', [':id' => $id]);
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyFaq($data, $id)
    {
        // P($data);
        $faq = self::find()->where('id = :id', [':id' => $id])->one();
        // P($faq);
        if(isset($data['Faq']['answer'])){
            $data['Faq']['answer'] = Html::encode($data['Faq']['answer']);
        }
        if(isset($data['Faq']['display'])){
            $data['Faq']['display'] = Tool::changeBool($data['Faq']['display']);
        }
        // P($data);
        if(!is_null($faq)){
            if($this->load($data) and $this->validate()){
                if(isset($this->question)){
                    $faq->question = $this->question;
                }
                if(isset($this->answer)){
                    $faq->answer = $this->answer;
                }
                if(isset($this->display)){
                    $faq->display = $this->display;
                }
                if($faq->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    
}